package com.apes.framework.plugin.workflow.parser.element;

import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;
import com.apes.framework.plugin.workflow.parser.model.Node;
import com.apes.framework.plugin.workflow.parser.node.WorkflowNodeParser;
import com.apes.framework.util.Tools;
import org.springframework.stereotype.Component;

/**
 * 功能：end元素解析器
 *
 * @author xul
 * @create 2018-06-30 11:36
 */
@Component
public class NodeElementParser implements ApesElementParser<ApesElement, Node> {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"start", "task", "end"};

    /**
     * 功能：可解析的节点或元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return WorkflowNodeParser.domain;
    }

    /**
     * 功能：元素解析
     *
     * @param element
     */
    @Override
    public Node parse(ApesElement element) {
        Node node = Tools.toBean(Node.class, element.getAttributes());
        node.setName(element.getName());

        ApesElement apesElement;


        if (element.getName().equalsIgnoreCase("task")) {
            // form 元素解析
            apesElement = getApesElement(element, "form");
            if (apesElement == null) apesElement = getApesElement(element.getParent(), "form");
            if (apesElement != null) node.setForm(apesElement.parse());

            // users 元素解析
            apesElement = getApesElement(element, "users");
            if (apesElement != null) node.setGroups(apesElement.parse());

            // fields 元素解析
            apesElement = getApesElement(element, "fields");
            if (apesElement != null) node.setFields(apesElement.parse());
        }

        if (!element.getName().equalsIgnoreCase("end")) {
            // mapping 元素解析
            apesElement = getApesElement(element, "mapping");
            if (apesElement != null) node.setConfig(apesElement.parse());

            // content 元素解析
            apesElement = getApesElement(element, "content");
            if (apesElement != null) node.setContent(apesElement.parse());
        }

        // executor 元素解析
        apesElement = getApesElement(element, "before");
        if (apesElement != null) {
            node.setBeforeEsbIds(apesElement.parse());
            if (node.getForm() != null) {
                node.getBeforeEsbIds().stream().forEach(esbId -> {
                    node.addFormEsbId(node.getForm().getId(), esbId);
                });
            }
        }

        apesElement = getApesElement(element, "after");
        if (apesElement != null) {
            node.setAfterEsbIds(apesElement.parse());
            if (node.getForm() != null) {
                node.getAfterEsbIds().stream().forEach(esbId -> {
                    node.addFormEsbId(node.getForm().getId(), esbId);
                });
            }
        }

        return node;
    }

    private ApesElement getApesElement(ApesElement element, String name) {
        try {
            return element.getNestedElements().stream().filter(e -> e.getName().equalsIgnoreCase(name)).findFirst().get();
        } catch (Exception e) {
            return null;
        }
    }
}
